// mainutil
package main

import (
	"fmt"
	"net"
	"strconv"
	"strings"
	"time"
)

func GetUtilIp() string {
	addrs, err := net.InterfaceAddrs()
	if err != nil {
		fmt.Println(err)
		//os.Exit(1)
		return ""
	}
	ipval := ""
	for _, address := range addrs {
		// 检查ip地址判断是否回环地址
		if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
			if ipnet.IP.To4() != nil {

				if ipval == "" {
					ipval = ipnet.IP.String()
				} else {
					arr := strings.Split(ipnet.IP.String(), ".")
					if arr[len(arr)-1] != "1" {
						ipval = ipnet.IP.String()
						break
					}
				}

			}

		}
	}
	return ipval
}

func GetUtilTime() string {
	t := time.Now().UnixNano()
	//fmt.Println(t)
	return strconv.FormatInt(t, 10)
}

func GetUtilClientKey() string {
	return GetUtilIp() + "-" + GetUtilTime()
}
